/*
 * Copyright (c) 2010-2025. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.commandhandling.annotations;

import org.axonframework.common.AxonConfigurationException;
import org.axonframework.messaging.annotations.HandlerAttributes;
import org.axonframework.messaging.Message;
import org.axonframework.messaging.annotations.HandlerEnhancerDefinition;
import org.axonframework.messaging.annotations.MessageHandlingMember;
import org.axonframework.messaging.annotations.WrappedMessageHandlingMember;
import org.axonframework.messaging.unitofwork.ProcessingContext;

import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Optional;
import jakarta.annotation.Nonnull;

/**
 * Implementation of a {@link HandlerEnhancerDefinition} that is used for {@link CommandHandler} annotated methods.
 *
 * @author Allard Buijze
 * @since 3.0.0
 */
public class MethodCommandHandlerDefinition implements HandlerEnhancerDefinition {

    @Override
    public <T> MessageHandlingMember<T> wrapHandler(@Nonnull MessageHandlingMember<T> original) {
        Optional<String> optionalRoutingKey = original.attribute(HandlerAttributes.COMMAND_ROUTING_KEY);
        Optional<String> optionalCommandName = original.attribute(HandlerAttributes.COMMAND_NAME);
        return optionalRoutingKey.isPresent() && optionalCommandName.isPresent()
                ? new MethodCommandHandlingMember<>(original,
                                                    optionalRoutingKey.get(),
                                                    optionalCommandName.get())
                : original;
    }

    private static class MethodCommandHandlingMember<T>
            extends WrappedMessageHandlingMember<T>
            implements CommandHandlingMember<T> {

        private final String commandName;
        private final boolean isFactoryHandler;
        private final String routingKey;

        private MethodCommandHandlingMember(MessageHandlingMember<T> delegate,
                                            String routingKeyAttribute,
                                            String commandNameAttribute) {
            super(delegate);
            Executable executable =
                    delegate.unwrap(Executable.class)
                            .orElseThrow(() -> new AxonConfigurationException(
                                    "The @CommandHandler annotation must be put on an Executable "
                                            + "(either directly or as Meta Annotation)"
                            ));

            routingKey = "".equals(routingKeyAttribute) ? null : routingKeyAttribute;
            commandName = "".equals(commandNameAttribute) ? delegate.payloadType().getName() : commandNameAttribute;
            final boolean factoryMethod = executable instanceof Method && Modifier.isStatic(executable.getModifiers());
            isFactoryHandler = executable instanceof Constructor || factoryMethod;
        }

        @Override
        public boolean canHandle(@Nonnull Message message, @Nonnull ProcessingContext context) {
            return super.canHandle(message, context);
        }

        @Override
        public String routingKey() {
            return routingKey;
        }

        @Override
        public String commandName() {
            return commandName;
        }

        @Override
        public boolean isFactoryHandler() {
            return isFactoryHandler;
        }
    }
}
